package com.lipiao.makerandroid.View.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lipiao.makerandroid.Bean.RespondBean.ReadBean;
import com.lipiao.makerandroid.R;
import com.lipiao.makerandroid.Utils.HttpUtil;
import com.lipiao.makerandroid.View.Activity.AddActivity;
import com.lipiao.makerandroid.View.Activity.ReadActivity;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MainFragment extends Fragment {

    static String userNumber;//账号

    RefreshLayout refreshLayout;

    String TAG = "MainFragment";
    View rootView;

    private Unbinder unbinder;

    private List<ReadBean> topDataBeanList;

    static List<ReadBean> mList = new ArrayList<>();

    FloatingActionButton floatingActionButton;

    public MainFragment() {

    }


    public static MainFragment newInstance(String username) {
        MainFragment fragment = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putString("userNumber", username);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void initView(View root){
        refreshLayout = root.findViewById(R.id.srl_sf2);
        refreshLayout.setReboundDuration(200);//回弹动画
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(500);
                initData();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_main, container, false);
        //返回一个Unbinder值（进行解绑），这里的this不能使用getActivity()
        unbinder = ButterKnife.bind(this, rootView);
        assert getArguments() != null;//判空处理
        userNumber = getArguments().getString("userNumber");//获取账号
        Log.d(TAG, "onCreateView: userNumber "+ userNumber);
//        System.out.println("-----------------");
        floatingActionButton = rootView.findViewById(R.id.fb_addNewRead);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getContext(), AddActivity.class);
                i.putExtra("userNumber",userNumber);
                startActivity(i);
            }
        });
        initData();
        initView(rootView);
        return rootView;
    }

    private void initData() {
//        refreshLayout.autoRefreshAnimationOnly();
//        refreshLayout.finishRefresh(1100/*,false*/);//传入false表示刷新失败//手动设置动画时长为1.1秒
        Call<ResponseBody> callTopArticle = HttpUtil.getUserService().getTenRead(userNumber);
        callTopArticle.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String strBack = null;
                try {
                    TextView textView11 = getActivity().findViewById(R.id.read_article_author1);
                    TextView textView12 = getActivity().findViewById(R.id.tv_top_article_title1);
                    TextView textView21 = getActivity().findViewById(R.id.read_article_author2);
                    TextView textView22 = getActivity().findViewById(R.id.tv_top_article_title2);
                    TextView textView31 = getActivity().findViewById(R.id.read_article_author3);
                    TextView textView32 = getActivity().findViewById(R.id.tv_top_article_title3);
                    TextView textView41 = getActivity().findViewById(R.id.read_article_author4);
                    TextView textView42 = getActivity().findViewById(R.id.tv_top_article_title4);
                    TextView textView51 = getActivity().findViewById(R.id.read_article_author5);
                    TextView textView52 = getActivity().findViewById(R.id.tv_top_article_title5);
                    TextView textView61 = getActivity().findViewById(R.id.read_article_author6);
                    TextView textView62 = getActivity().findViewById(R.id.tv_top_article_title6);

                    strBack = response.body().string();
//                    System.out.println(strBack);
//                    System.out.println("-----------------------");
                    //处理数据
                    JSONArray jsonObject = null;
                    jsonObject = new JSONArray(strBack);
                    Gson gson = new Gson();
//                    System.out.println(jsonObject.toString());
                    topDataBeanList = gson.fromJson(jsonObject.toString(), new TypeToken<List<ReadBean>>(){}.getType());
//                    初始化mList
                    for (int i = 0; i < topDataBeanList.size(); i++) {
                        //文章所需数据 mList
                        ReadBean readBean = new ReadBean(
                                topDataBeanList.get(i).getReadId(),
                                "" + topDataBeanList.get(i).getReadText(),
                                "" + topDataBeanList.get(i).getUserNumber()
                        );
                        mList.add(readBean);
                    }
//                    Toast.makeText(MainFragment.this.getActivity(),topDataBeanList.get(1).getUserNumber(),Toast.LENGTH_LONG).show();
                    textView11.setText(topDataBeanList.get(0).getUserNumber());
                    textView12.setText(topDataBeanList.get(0).getReadText());
                    textView21.setText(topDataBeanList.get(1).getUserNumber());
                    textView22.setText(topDataBeanList.get(1).getReadText());
                    textView31.setText(topDataBeanList.get(2).getUserNumber());
                    textView32.setText(topDataBeanList.get(2).getReadText());
                    textView41.setText(topDataBeanList.get(3).getUserNumber());
                    textView42.setText(topDataBeanList.get(3).getReadText());
                    textView51.setText(topDataBeanList.get(4).getUserNumber());
                    textView52.setText(topDataBeanList.get(4).getReadText());
                    textView61.setText(topDataBeanList.get(5).getUserNumber());
                    textView62.setText(topDataBeanList.get(5).getReadText());
                    textView12.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent i = new Intent(getContext(), ReadActivity.class);
                            i.putExtra("text",topDataBeanList.get(0).getReadText());
                            i.putExtra("userNo",topDataBeanList.get(0).getUserNumber());
                            i.putExtra("readId",topDataBeanList.get(0).getReadId());
                            System.out.println(topDataBeanList.get(0).getReadId()+"====================");
                            i.putExtra("userNumber",userNumber);
                            getActivity().startActivity(i);
                        }
                    });
                    textView22.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent i = new Intent(getContext(), ReadActivity.class);
                            i.putExtra("text",topDataBeanList.get(1).getReadText());
                            i.putExtra("userNo",topDataBeanList.get(1).getUserNumber());
                            i.putExtra("readId",topDataBeanList.get(1).getReadId());
                            i.putExtra("userNumber",userNumber);
                            getActivity().startActivity(i);
                        }
                    });
                    textView32.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent i = new Intent(getContext(), ReadActivity.class);
                            i.putExtra("text",topDataBeanList.get(2).getReadText());
                            i.putExtra("userNo",topDataBeanList.get(2).getUserNumber());
                            i.putExtra("readId",topDataBeanList.get(2).getReadId());
                            i.putExtra("userNumber",userNumber);
                            getActivity().startActivity(i);
                        }
                    });
                    textView42.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent i = new Intent(getContext(), ReadActivity.class);
                            i.putExtra("text",topDataBeanList.get(3).getReadText());
                            i.putExtra("userNo",topDataBeanList.get(3).getUserNumber());
                            i.putExtra("readId",topDataBeanList.get(3).getReadId());
                            i.putExtra("userNumber",userNumber);
                            getActivity().startActivity(i);
                        }
                    });
                    textView52.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent i = new Intent(getContext(), ReadActivity.class);
                            i.putExtra("text",topDataBeanList.get(4).getReadText());
                            i.putExtra("userNo",topDataBeanList.get(4).getUserNumber());
                            i.putExtra("readId",topDataBeanList.get(4).getReadId());
                            i.putExtra("userNumber",userNumber);
                            getActivity().startActivity(i);
                        }
                    });
                    textView62.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent i = new Intent(getContext(), ReadActivity.class);
                            i.putExtra("text",topDataBeanList.get(5).getReadText());
                            i.putExtra("userNo",topDataBeanList.get(5).getUserNumber());
                            i.putExtra("readId",topDataBeanList.get(5).getReadId());
                            i.putExtra("userNumber",userNumber);
                            getActivity().startActivity(i);
                        }
                    });


                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
        //System.out.println(topDataBeanList.size());
    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }



}
